package com.liurui.demo.dao;

import com.liurui.demo.pojo.Department;
import com.liurui.demo.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository //Dao层的Bean注册
public class EmployeeDao {
    //模拟数据库中的数据
    private static Map<Integer, Employee> employees=null;

    @Autowired  //自动注入, 员工有所属部门
    private DepartmentDao departmentDao;

    static {
        employees = new HashMap<Integer,Employee>();//创建一个员工表
        employees.put(1001,new Employee(1001,"lili","123456789@qq.com",1,new Department(101,"教学部")));
        employees.put(1002,new Employee(1002,"runtu","213456789@qq.com",0,new Department(101,"教学部")));
        employees.put(1003,new Employee(1003,"longge","321456789@qq.com",1,new Department(102,"市场部")));
        employees.put(1004,new Employee(1004,"hanxinmeng","132456789@qq.com",0,new Department(101,"教学部")));
        employees.put(1005,new Employee(1005,"luxiao","231456789@qq.com",1,new Department(104,"运营部")));
    }

    public static Integer initId=1006;
    //增加一个员工，也可用于更新员工
    //因为前端只是传进来一个department的id，没有具体的部门名称，所以要给用户的部门名称赋值
    public void insert(Employee employee){
        if(employee.getId()==null){//员工id不存在,即新增员工，非更新员工
            employee.setId(initId++);
        }
        //设置员工的部门(部门数据库.跟据部门id键获取部门(员工.获取部门.跟据部门id))
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);
    }

    //查询所有员工
    public Collection<Employee> getEmployees(){
        return employees.values();
    }

    //跟据id查询单个员工
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }

    //跟据id删除单个员工
    public void deleteEmployeeById(Integer id){
        employees.remove(id);
    }
}
